package cn.itsource.org.controller;

import cn.itsource.basic.util.AjaxResult;
import cn.itsource.basic.util.PageList;
import cn.itsource.org.domain.Department;
import cn.itsource.org.query.DepartmentQuery;
import cn.itsource.org.service.IDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 以后叫写接口
 * 部门接口   restFull风格的接口
 * 以后写接口需要考虑
 *   1.请求地址是什么  URL
 *   2.做什么操作
 *   3.返回什么值
 */
@RestController
@RequestMapping("/dept")
/*@GetMapping
@PostMapping
@DeleteMapping
@PutMapping
@PatchMapping*/
@Api(tags = "部门接口",description = "部门接口详细描述")
//@CrossOrigin
public class DepartmentController {
    @Autowired
    IDepartmentService departmentService;

    // 新增和修改使用：@PutMapping  通过有没有Id来确定是不是修改
    @PutMapping
    @ApiOperation(value = "部门添加或修改",notes = "如果有id就是修改否则就是添加")
    public AjaxResult addOrUpdate(@RequestBody Department department){
        try {
            if (department.getId() != null) {
                departmentService.update(department);
            }else{
                departmentService.add(department);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me()
                    .setMessage("操作失败！"+e.getMessage());
        }
    }




    //删除数据    @DeleteMapping("/{id}")
    // 127.0.0.1/dept/9
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除部门",notes = "需要传递ID")
    public AjaxResult deleteById(@PathVariable("id") Long id){
        /* 第一种：
        try {
            departmentService.delete(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult("操作失败"+e.getMessage());
        }*/

        /* 稍微高级一点点
        try {
            departmentService.delete(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            AjaxResult me = AjaxResult.me();
            me.setSuccess(false);
            me.setMessage("操作失败"+e.getMessage());
            return me;
        }*/
        /*链式编程思维*/
        try {
            departmentService.delete(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me()
                    .setMessage("操作失败！"+e.getMessage());
        }
    }

    /**
     * 在restFull风格接口中，使用{}占位
     * 那么在取值的时候，使用@PathVariable来获取占位的值
     * @param id
     * @return
     */
    @GetMapping("/{id}")//http://127.0.0.1:8080/dept/1
    public Department queryById(@PathVariable("id") Long id){
        return departmentService.queryById(id);
    }

    //查询所有  @GetMapping
    @GetMapping//http://127.0.0.1:8080/dept/
    public List<Department> queryAll(){
        return departmentService.queryAll();
    }
    //查询所有  @GetMapping
    @GetMapping("/deptTree")
    public List<Department> deptTree(){
        return departmentService.deptTree();
    }

    @GetMapping("/deptTrees")
    public List<Department> deptTrees(){
        return departmentService.deptTrees();
    }
    //分页查询
    @PostMapping
    public PageList<Department> queryPage(@RequestBody DepartmentQuery query){
        return departmentService.queryPage(query);
    }


    @PatchMapping
    /*
        1.前台传入的参数 如果是数组  SpringMVC 接收的方式  1.数组  2.集合
        解决方案： 1.循环 [8,7,5]
                   2.拼接动态sql delete from 表 where id in( 8,7,5)
     */
    public AjaxResult deleteByIds(@RequestBody Long[] ids){
        try {
           departmentService.deleteByIds(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me()
                    .setMessage("操作失败！"+e.getMessage());
        }

    }
}
